/**
 * 
 */
package acmCoder.ctrip;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * @author zhongfang
 *
 */
public class path {

	static class Node {
		int depth = 0;
		int fromCount = 0;
		ArrayList<Node> next = new ArrayList<>();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		while (scanner.hasNext()) {
			int n = scanner.nextInt();
			int m = scanner.nextInt();
			int[][] matrix = new int[n][m];

			Node[][] nodes = new Node[n][m];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					nodes[i][j] = new Node();
					matrix[i][j] = scanner.nextInt();
				}
			}
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					if (i != 0) {// 左侧
						addLink(nodes, i, j, i - 1, j, matrix);
					}
					if (j != 0) {// 上册
						addLink(nodes, i, j, i, j - 1, matrix);
					}
					if (i != n - 1) {
						addLink(nodes, i, j, i + 1, j, matrix);
					}
					if (j != m - 1) {
						addLink(nodes, i, j, i, j + 1, matrix);
					}
				}
			}
			// 拓扑排序
			Queue<Node> queue = new LinkedList<>();
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					if (nodes[i][j].fromCount == 0) {
						nodes[i][j].depth = 1;
						queue.add(nodes[i][j]);
					}
				}
			}
			int max = 0;
			while (!queue.isEmpty()) {
				Node node = queue.poll();
				for (Node neibor : node.next) {
					neibor.depth = Math.max(neibor.depth, node.depth + 1);
					max = Math.max(max, neibor.depth);
					neibor.fromCount--;
					if (neibor.fromCount == 0) {
						queue.add(neibor);
					}
				}
			}
			System.out.println(max);
		}
	}

	private static void addLink(Node[][] nodes, int i, int j, int newI, int newJ, int[][] matrix) {
		if (matrix[i][j] < matrix[newI][newJ]) {
			link(nodes[i][j], nodes[newI][newJ]);
		} else if (matrix[i][j] > matrix[newI][newJ]) {
			link(nodes[newI][newJ], nodes[i][j]);
		}
	}

	private static void link(Node node, Node node2) {
		node.next.add(node2);
		node2.fromCount++;
	}

}
